﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace TfL
{
    //[Serializable]
    [XmlRoot("ROOT")]
    public class TrainTimes
    {
        public TrainTimes(string line)
        {
            Line = line;
            Stations = new Dictionary<string, TrainTimesForStation>();
        }

        public TrainTimes()
        {
            Line = string.Empty;
            Stations = new Dictionary<string, TrainTimesForStation>();
        }

        public string Line { get; set; }

        [XmlArray("")]
        public Dictionary<string,TrainTimesForStation> Stations { get; set; }

        public void Parse(XmlReader reader)
        {

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                    break;
                if (reader.Name == "S")
                {
                    TrainTimesForStation station = new TrainTimesForStation(Line);
                    station.Parse(reader);
                    Stations.Add(station.Name,station);
                }
            }
        }

        public IEnumerable<TrainTimesForStation> Parse(Stream stream)
        {
            try
            {

           
                XDocument document = XDocument.Load(stream);
                XNamespace ns = XNamespace.Get("");
                Stations = (from station in document.Descendants(ns + "S")
                            select new KeyValuePair<string, TrainTimesForStation>(
                                station.Attribute("N").Value,
                                new TrainTimesForStation(Line)
                                    {
                                        Code = station.Attribute("Code").Value,
                                        Name = station.Attribute("N").Value,
                                        Platforms = (from platform in station.Descendants("P")
                                                     select new Platform
                                                                {
                                                                    Name = platform.Attribute("N").Value,
                                                                    Code = int.Parse(platform.Attribute("Code").Value),
                                                                    Trains = (from train in platform.Descendants("T")
                                                                              select new Train
                                                                                         {
                                                                                             Destination = train.Attribute("DE").Value,
                                                                                             TimeToStation = train.Attribute("C").Value == "-"? TimeSpan.FromSeconds(0):
                                                                                                 TfL.ExtensionMethods.ParseTfl(train.Attribute("C").Value),
                                                                                             Location = train.Attribute("L").Value
                                                                                         }).ToList()
                                                                }).ToList()

                                    })).ToDictionary((kvp)=>kvp.Key,(kvp)=>kvp.Value);
                return Stations.Values;
            }
            catch (Exception)
            {

                return new List<TrainTimesForStation>();
            }

        }

        public override string ToString()
        {
            return string.Format("{0}: {1} stations\n\r\t {2}", Line, Stations.Count, Stations.Aggregate("",(seed,station)=> seed+"\n\r\t"+station.Value));
        }
    }
}
